package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.zhidao.model.po.Admin;
import com.example.zhidao.model.po.Banner;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.AdminService;
import com.example.zhidao.service.BannerService;
import com.example.zhidao.utils.Ujwt;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/banner")
public class BannerController {//7

    @Autowired
    private BannerService bannerService;

    @Autowired
    private Ujwt ujwt;

    @Autowired
    private AdminService adminService;



//    获取轮播的信息
    @GetMapping("/app")
    private ResponseData getbanner(){
        QueryWrapper<Banner> qw=new QueryWrapper<>();
        qw.eq("type",2);
        List<Banner> bannerList= bannerService.list(qw);
        if(bannerList==null){
            return new ResponseData(bannerList,false,"获取失败",201);
        }else{
        return new ResponseData(bannerList,true,"获取成功",200);
        }
    }

//    预览
    @GetMapping("/image")
    public byte[] getImage(HttpServletResponse response, String img) throws Exception {
//        File file = new File("F:/202202/upload1/" + img);
//    String path = "/var/lib/tomcat8/upload/";
    File file = new File("/home/ubuntu/blog/upload/" + img);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }



//    修改图片的类型
    @PatchMapping
    public ResponseData updatebanner(String id ,int type){
        UpdateWrapper<Banner> uw=new UpdateWrapper<>();
        uw.eq("id",id);
        uw.set("type",type);
        boolean i= bannerService.update(uw);
        if(i){
            return new ResponseData(i,true,"修改成功",200);
        }else{
            return new ResponseData(i,false,"修改失败",201);
        }
    }



//    ---------------------------------------------------------------------------------------------------------------
//    以下为管理员专用的接口



    //    获取app轮播的信息
    @GetMapping("/admin/app")
    private ResponseData getbannerapp(){
        QueryWrapper<Banner> qw=new QueryWrapper<>();
        qw.eq("type",2);
        List<Banner> bannerList= bannerService.list(qw);
        if(bannerList==null){
            return new ResponseData(bannerList,false,"获取失败",201);
        }else{
            return new ResponseData(bannerList,true,"获取成功",200);
        }
    }

//    获取pc短的轮播图
@GetMapping("/admin/pc")
private ResponseData getbannerpc(){
    QueryWrapper<Banner> qw=new QueryWrapper<>();
    qw.eq("type",1);
    List<Banner> bannerList= bannerService.list(qw);
    if(bannerList==null){
        return new ResponseData(bannerList,false,"获取失败",201);
    }else{
        return new ResponseData(bannerList,true,"获取成功",200);
    }
}


//    上传轮播图

    @PostMapping("/admin/add")
    private ResponseData uploadBanner(@RequestParam(value = "type") int type,MultipartFile upload){

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");//自动生成uuid
        Banner banner=new Banner();
        banner.setId(uuid);
        banner.setType(type);

        String fileName = upload.getOriginalFilename();
        banner.setImageurl(fileName);
//        String path = "F:/202202/upload1/";
         String path = "/home/ubuntu/blog/upload/"; //阿里云
        File file = new File(path); //3. 判断路径是否存在，不存在则新建
        if (!file.exists()){
            file.mkdir();
        }
        // 3.改名,避免重名,定义新文件的名字
        // String newName = path+File.separator+System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        String message="图片上传失败";
        try {
            upload.transferTo(new File(path+fileName));
            message= "上传成功";
        } catch (IOException e) {
            message=message+e.toString();
            return new ResponseData(-1, false, message, 200);
        }

        boolean i =bannerService.save(banner);
        if (i)
            return new ResponseData(i, true,  message+"记录增加成功", 200);
        else
            return new ResponseData(-1, false, "请求失败", 200);
    }


    @DeleteMapping
    public ResponseData delabnner(@RequestHeader String token,String id){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }

        boolean i=bannerService.removeById(id);

        if(i){
            return new ResponseData(null,true,"成功删除",200);
        }else{
            return new ResponseData(null,false,"删除失败",200);
        }

    }

}
